#!/bin/sh

export MC="-j$(nproc)"

echo
echo "============================================"
echo "Install extensions from   : install.sh"
echo "PHP version               : ${PHP_VERSION}"
echo "Extra Extensions          : ${PHP_EXTENSIONS}"
echo "Extra Extensions Dir      : ${PHP_CONF_DIR}"
echo "Multicore Compilation     : ${MC}"
echo "Work directory            : ${PWD}"
echo "============================================"
echo


if [ "${PHP_EXTENSIONS}" != "" ]; then
    yum install -y gcc gcc-c++  make autoconf 
fi


export EXTENSIONS=",${PHP_EXTENSIONS},"


#
# Install extension from package file(.tgz),
# For example:
#
# installExtensionFromTgz redis-5.0.2
#
# Param 1: Package name with version
# Param 2: enable options
#
installExtensionFromTgz()
{
    tgzName=$1
    extensionName="${tgzName%%-*}"

    mkdir ${extensionName}
    tar -xf ${tgzName}.tgz -C ${extensionName} --strip-components=1
    ( cd ${extensionName} && phpize && ./configure && make ${MC} && make install )
}

#
# Add Conf File
# For example:
#
# installExtensionConf redis
#
# Param 1: Package name
#
installExtensionConf()
{
    extensionName=$1
    echo -e "; ${extensionName} extension ;\nextension=${extensionName}.so" > ${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini
    echo "----------- Add Conf File ---------------"
    echo "${PHP_CONF_DIR}/docker-php-ext-${extensionName}.ini";
}

#
# Install extension from php source file,
# For example:
#
# installExtensionFromExt pcntl
#
# Param 1: Package name
# Param 2: enable options
#
installExtensionFromExt()
{
    extensionName=$1
    extensionDIR="/usr/local/src/php-${PHP_VERSION}/ext/${extensionName}"

    ( cd ${extensionDIR} && phpize && ./configure && make ${MC} && make install )
}


if [[ -z "${EXTENSIONS##*,redis,*}" ]]; then
    echo "---------- Install redis ----------"
    installExtensionFromTgz redis-4.1.1
    installExtensionConf redis
fi

if [[ -z "${EXTENSIONS##*,memcached,*}" ]]; then
    echo "---------- Install memcached ----------"
    yum install -y libmemcached libmemcached-devel
    installExtensionFromTgz memcached-2.2.0
    installExtensionConf memcached

fi

# if [[ -z "${EXTENSIONS##*,xdebug,*}" ]]; then
#     echo "---------- Install xdebug ----------"
#     isPhpVersionGreaterOrEqual 7 0

#     if [[ "$?" = "1" ]]; then
#         isPhpVersionGreaterOrEqual 7 4
#         if [[ "$?" = "1" ]]; then
#             installExtensionFromTgz xdebug-2.9.2
#         else
#             installExtensionFromTgz xdebug-2.6.1
#         fi
#     else
#         installExtensionFromTgz xdebug-2.5.5
#     fi
# fi

if [[ -z "${EXTENSIONS##*,mongodb,*}" ]]; then
    echo "---------- Install mongodb ----------"
    installExtensionFromTgz mongodb-1.5.5
    installExtensionConf mongodb
fi

if [[ -z "${EXTENSIONS##*,pcntl,*}" ]]; then
    echo "---------- Install pcntl ----------"
    installExtensionFromExt pcntl
    installExtensionConf pcntl
fi

if [[ -z "${EXTENSIONS##*,sysvsem,*}" ]]; then
    echo "---------- Install sysvsem ----------"
    installExtensionFromExt sysvsem
    installExtensionConf sysvsem
fi

if [[ -z "${EXTENSIONS##*,sysvshm,*}" ]]; then
    echo "---------- Install sysvshm ----------"
    installExtensionFromExt sysvshm
    installExtensionConf sysvshm
fi

if [[ -z "${EXTENSIONS##*,sysvmsg,*}" ]]; then
    echo "---------- Install sysvmsg ----------"
    installExtensionFromExt sysvmsg
    installExtensionConf sysvmsg
fi

if [[ -z "${EXTENSIONS##*,opcache,*}" ]]; then
    echo "---------- Install opcache ----------"
    installExtensionFromExt opcache
    installExtensionConf opcache
fi

if [[ -z "${EXTENSIONS##*,swoole,*}" ]]; then
    echo "---------- Install swoole ----------"
    installExtensionFromTgz swoole-2.0.11
    installExtensionConf swoole
fi

if [[ -z "${EXTENSIONS##*,zip,*}" ]]; then
    echo "---------- Install zip ----------"
    yum install -y libzip-devel
    installExtensionFromExt zip
	installExtensionConf zip
fi
